package com.app.micaihu.view.newsdetail.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.bean.news.RecommendGalleryEntity;
import com.app.micaihu.view.newsdetail.adapter.ImageDetailAboutAdapter;

import java.util.List;
/**
 * Created by Guo on 2016/7/29
 * 图库详情的View->只能在代码中写
 */
public class ImageAboutView extends LinearLayout implements View.OnClickListener, AdapterView.OnItemClickListener {

    private List list ;
    private String nextArticleId;
    private ImageDetailAboutAdapter imageDetailAboutAdapter;
    private OnImageDetailAboutClickListener onImageDetailAboutClickListener;
    private GridView gvGridView;
    private Context context;

    public ImageAboutView(Context context, List list, String nextArticleId) {
        super(context);
        this.context = context;
        this.list = list;
        this.nextArticleId = nextArticleId;
        initView(context);
    }
    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_about_image_page,null);
        TextView tvNext = (TextView) view.findViewById(R.id.tv_next);
        TextView tvChange = (TextView) view.findViewById(R.id.tv_change);
        gvGridView = (GridView) view.findViewById(R.id.gv_gridview);

        if(TextUtils.isEmpty(nextArticleId)){
            tvNext.setVisibility(View.GONE);
        }else{
            tvNext.setVisibility(View.VISIBLE);
        }


        if(tvNext != null) tvNext.setOnClickListener(this);
        if(tvChange != null) tvChange.setOnClickListener(this);
        if(gvGridView != null) gvGridView.setOnItemClickListener(this);
        //
        if(imageDetailAboutAdapter == null){
            imageDetailAboutAdapter = new ImageDetailAboutAdapter(list,context);
            gvGridView.setAdapter(imageDetailAboutAdapter);
        }else{
            imageDetailAboutAdapter.notifyDataSetChanged();
        }

        addView(view);

    }


    @Override
    public void onClick(View v) {
        if(v == null) return;
        switch(v.getId()){
            case R.id.tv_next:
                if(onImageDetailAboutClickListener != null){
                    onImageDetailAboutClickListener.nextImage();
                }
                break;
            case R.id.tv_change:
                if(onImageDetailAboutClickListener != null){
                    onImageDetailAboutClickListener.change();
                }
                break;

        }
    }


    /****************************************相关推荐的回调*******************************************************/

    public interface OnImageDetailAboutClickListener{
        void change();
        void nextImage();
        void gridViewClick(View view);
    }

    public void setOnImageDetailAboutClickListener(OnImageDetailAboutClickListener onImageDetailAboutClickListener){
        this.onImageDetailAboutClickListener = onImageDetailAboutClickListener;
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(onImageDetailAboutClickListener != null){
            onImageDetailAboutClickListener.gridViewClick(view);
        }

    }


    /**
     * 外部调用的刷新相关推荐的列表
     * @param list
     */
    public void notifyData(List<RecommendGalleryEntity> list){
        this.list.clear();
        this.list.addAll(list);

        if(imageDetailAboutAdapter != null){
            imageDetailAboutAdapter.notifyDataSetChanged();
        }else{
            imageDetailAboutAdapter = new ImageDetailAboutAdapter(list,context);
            gvGridView.setAdapter(imageDetailAboutAdapter);
        }


    }
}
